var ns;
app = new Vue({
    el: '#app',
    data: {
        showsearch: false,
        showsearchmonth: false,
        showsearchparam: false,
        ProjectName:'',
        form: {
            startMonth: '', 
            activeDateDesc: '月份',
            ProjectCompanyType: 1,
            ProjectID:0,
            ProjectIDs: [],
        },
        summary: {},
        projectList: [], 
        XJlist:[],  ////图表数据
        UserRankList:{
            Complate:[],//按任务完成数
            UnComplate:[],//按任务未完成数
            OverTime:[],//按任务超时完成数
            Point:[],//按工作点完成数
        },
        userList:[],  ////员工工时
        formuser:{
            ID:0,
            Name:'按任务正常完成数',
        },
        userTypeID:0,
        userType:[
            {ID:0,Name:'按任务正常完成数'},
            {ID:1,Name:'按任务未完成数'},
            {ID:2,Name:'按任务超时完成数'},
            {ID:3,Name:'按工作点完成数'},
        ],
        catedata:[],//日期数组
        finishdata:[],//数组
        missdata:[],
        minDate: new Date(),
        maxDate: new Date(),
        currentDate:0,
        LSsummary:{},
    },
    methods: {
        formatter(type, val) {
            if (type === 'year') {
                return `${val}年`;
            }
            if (type === 'month') {
                return `${val}月`;
            }
            return val;
        },
        onConfirm(day) {
            var that = this;
            var nowdate=moment(day).format('YYYY-MM');
            that.form.startMonth = nowdate;
            that.doSearch();
        },
        open_task:function(){
            var that = this;
            var title='环境保洁任务列表';
            var url='bjlh_tasklist';
            ns.openWin(url, title, {
                ProjectID: that.form.ProjectID,
                ProjectName:that.ProjectName,
                startMonth:that.form.startMonth,
                IsAnalysis:true,
            });
        },
        get_data: function () {
            var that = this;
            that.getSearchParams();
        },
        doSelectProject: function (ID,Name) {
            var that = this;
            that.ProjectName=Name;
            that.form.ProjectID=ID;
            that.form.ProjectIDs=[];
            that.form.ProjectIDs.push(ID);
            that.showsearchparam = false;
            that.doSearch();
        },
        //筛选数据
        getSearchParams: function () {
            var that = this;
            var options = {};
            options.action = "gettaskanalysisparams";
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    that.projectList = data.projectList;
                    if(that.projectList!=undefined&&that.projectList.length>0){
                        that.doSelectProject(that.projectList[0].ID,that.projectList[0].Name); 
                    }
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        doSelectSearchParam: function (type) {
            var that = this;
            if (type == 1) { //筛选
                that.showsearchparam = !that.showsearchparam;
                that.showsearchmonth = false;
            }
            if (type == 2) { //按月
                that.showsearchparam = false;
                that.showsearchmonth = !that.showsearchmonth;
               
            }
            that.showsearch = that.showsearchparam || that.showsearchmonth;
            if (type == 2) { //按月 用的van-popup 自带了遮罩
                that.showsearch=false;
            }
        },
        doHideSearch: function () {
            var that = this;
            that.showsearchparam = false;
            that.showsearchmonth = false;
            that.showsearch = false;
        },
        doSearch: function () {
            var that = this;
            that.doHideSearch();
            that.getAnalysisData();
        },
        //员工排行值改变事件
        onChangeType:function(){
            var that=this;
            that.userList=[];
            if(that.userTypeID==0){
                that.userList = that.UserRankList.Complate;//默认赋值第一个
            }
            if(that.userTypeID==1){
                that.userList = that.UserRankList.UnComplate;
            }
            if(that.userTypeID==2){
                that.userList = that.UserRankList.OverTime;
            }
            if(that.userTypeID==3){
                that.userList = that.UserRankList.Point;
            }  
        },
        getAnalysisData: function () {
            var that = this;
            //初始化
            that.XJlist=[];
            that.userList=[];
            var options = {};
            options.action = "get_user_environment_clean_plan_chart"; ////方法名称
            options.ProjectID = JSON.stringify(that.form.ProjectIDs);
            options.Month = that.form.startMonth;
            ns.post(options, function(succeed, data, err) {
                if (succeed) {
                    if(data.status){
                        that.LSsummary=data.DataList;
                        that.summary=data.info;
                        that.UserRankList=that.summary.UserRankList;
                        that.userList = that.UserRankList.Complate;//默认赋值第一个
                        that.XJlist = that.summary.PieChart;
                        if(that.XJlist[0].y<=0&&that.XJlist[1].y<=0&&that.XJlist[2].y<=0&&that.XJlist[3].y<=0&&that.XJlist[4].y<=0){
                            that.XJlist=[];
                        }
                        that.catedata=that.summary.PieChart;
                        that.loadChart("containerzx");//加载图表
                    }               
                } 
                else if(err){
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom '
                    });
                    return
                }
            }, {
                toast: true
            });
        },
        loadChart: function (IdName) {
            var that=this;
            var chart = new Highcharts.Chart(IdName, {
                chart: {
                    plotBackgroundColor: null,
                    plotBorderWidth: null,
                    plotShadow: false,
                    type: 'pie'
                },
                title: {
                    text: ''
                },
                credits: {
                    enabled: false //不显示Highcharts
                },
                //不显示右上角
                navigation: {
                    buttonOptions: {
                        enabled: false
                    }
                },
                colors:['#60b565','#ffcf48','#ff8033','#5894ff','#72cdd7',],
                tooltip: {
                    crosshairs: true,
                    shared: true,
                    headerFormat: '',
                    pointFormat: '<span style="color:{point.color}">\u25CF</span> {point.name}  {point.y}',
                },
                plotOptions: {
                    pie: {
                        allowPointSelect: true,
                        cursor: 'pointer',
                        dataLabels: {
                            // enabled: false,
                            enabled: true,
			                inside: true,
                            shadow:false,
			                distance: 10,
			                format: '{y}'
                        },
                        showInLegend: true
                    }
                },
                series: [{
                    name: '',
                    colorByPoint: true,
                    data: that.catedata,
                }]
            });
        },
        //跳转临时巡查记录页面
        open_task_time:function(){
            var that = this;
            var title='临时工作记录';
            var url='bjlh_tasklist_record';
            ns.openWin(url, title, {
                ProjectID: that.form.ProjectID,
                ProjectName:that.ProjectName,
                startMonth:that.form.startMonth,
                IsAnalysis:true,
            });
        },
    }
});
apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;
    app.minDate=new Date(moment(new Date()).add(-20, 'y'));
    app.maxDate=new Date(moment(new Date()).add(50, 'y'));
    app.currentDate=new Date();
    var nowdate=moment(app.currentDate).format('YYYY-MM');
    app.form.startMonth = nowdate;

    app.get_data();

};
